package org.example.memory;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.output.Response;

/**
 * @Author codeYang
 * @Date 2025/3/16 18:19
 * @Version 1.0
 */
public class ChatMemory {

    public static void main(String[] args) {

        ChatLanguageModel chatLanguageModel = OpenAiChatModel.builder().apiKey("demo")
                .modelName("gpt-4o-mini")
                .build();

        // 创建会话记录
        MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder().maxMessages(10).build();

        UserMessage userMessage1 = new UserMessage("你好，我是⼩明，你是我最好的兄弟，叫做阿强");
        chatMemory.add(userMessage1);
        AiMessage aiMessage = chatLanguageModel.generate(chatMemory.messages()).content();
        String answer1 = aiMessage.text();

        System.out.println(answer1);

        UserMessage userMessage2 = new UserMessage("小明最好的朋友是谁？");
        chatMemory.add(userMessage2);
        AiMessage aiMessage2 = chatLanguageModel.generate(chatMemory.messages()).content();
        String answer2 = aiMessage2.text();
        System.out.println(answer2);


    }
}
